package com.winchain.chainbill.receivables.biz.mod.rx;

import com.shell.bee.base.utils.DateUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.shell.framework.utils.CommonDataUtil;
import com.winchain.chainbill.receivables.base.po.ArModification;
import com.winchain.chainbill.receivables.base.po.ArOperation;
import com.winchain.chainbill.receivables.base.po.ArRecord;
import com.winchain.chainbill.receivables.base.service.ArModService;
import com.winchain.chainbill.receivables.base.service.ArOptService;
import com.winchain.chainbill.receivables.base.service.ArRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ReceivablesModSubscriber extends OnErrorNotImplementedSubscriber<ReceivablesModEvent> {
	@Autowired
	private ArRecordService arRecordService;
	@Autowired
	private ArOptService arOptService;
	@Autowired
	private ArModService arModService;

	public void onApplicationEvent(ReceivablesModEvent t) {
		ArRecord record = t.getRec();
		ArModification mod = t.getMod();

		ArOperation opt = t.getOperation();
		String bizId = record.getArId();
		String state = record.getArState();
		String nextState = opt.getNextState();

		int seqNo = record.getOptSeq().intValue();
		seqNo++;
		record.setOptSeq(Integer.valueOf(seqNo));
		record.setArState(nextState);

		this.arRecordService.saveOrUpdate(record);

		mod.setOptSeq(Integer.valueOf(seqNo));
		this.arModService.save(mod);

		opt.setOptId(SystemUtil.getUUID());
		opt.setBizId(bizId);
		opt.setState(state);
		opt.setNextState(nextState);
		opt.setOptTime(DateUtil.getSystemDateTime());
		opt.setOprId(CommonDataUtil.getUserId());
		opt.setSeqNo(Integer.valueOf(seqNo));
		this.arOptService.save(opt);
	}
}
